﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data.OleDb;


namespace Appsters
{
    public partial class Tasks : Form
    {
        private OleDbConnection dbConn;
        private DailyView dv = new DailyView();
        private DateTime lDate;
        private bool clickedA = false, clickedB = false;
        private string alm;
        private string rpt;
        private string ivl;
        private string oldTitle;
        private string oldSubj;
        private string oldDesc;
        private string newTitle;
        private string newSubj;
        private string newDesc;

        public Tasks(String theDate, DateTime longDate)
        {
            InitializeComponent();
            newSave.Hide();

            OleDbConnectionStringBuilder csb = new OleDbConnectionStringBuilder();

            csb["provider"] = "Microsoft.ACE.OLEDB.12.0";
            csb["data source"] = Application.StartupPath + @"\Jotter.accdb";

            string connStr = csb.ConnectionString;
            dbConn = new OleDbConnection(connStr);
            date = theDate;
            this.lDate = longDate;            

            radioButton2.Checked = true;
            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0;
        }

        public Tasks(string title, string subject, string description, string alarm, string repeat, string interval)
        {
            InitializeComponent();

            OleDbConnectionStringBuilder csb = new OleDbConnectionStringBuilder();

            csb["provider"] = "Microsoft.ACE.OLEDB.12.0";
            csb["data source"] = Application.StartupPath + @"\Jotter.accdb";
            string connStr = csb.ConnectionString;
            dbConn = new OleDbConnection(connStr);
            
            this.title.Text = title;
            this.subj.Text = subject;
            this.desc.Text = description;
            this.comboBox1.Text = repeat;
            this.comboBox2.Text = interval;

            try
            {
                if(alarm == "No Alarm")
                {
                    radioButton2.Checked = true;
                }
                else
                {
                    radioButton1.Checked = true;
                    String[] temp = alarm.Split(':');
                    label1.Text = temp[0];
                    String[] temp2 = temp[1].Split(' ');
                    Char[] temp3 = temp2[0].ToCharArray();
                    label3.Text = temp3[0].ToString();
                    label4.Text = temp3[1].ToString();
                    label5.Text = temp2[1];
                }                
            } catch(Exception t)
            {
                //ignore
            }
            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0;

            oldTitle = title;
            oldSubj = subject;
            oldDesc = description;
            updateMe();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button3_Click(object sender, EventArgs e)
        {
             this.WindowState = FormWindowState.Minimized;

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            clickedA = true;
            clickedB = false;

            System.Reflection.Assembly myAssembly;
            myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
            System.IO.Stream file;   
            Bitmap image;
            file = myAssembly.GetManifestResourceStream("Appsters.background.png");
            image = new Bitmap(file);
            this.BackgroundImage = image;

            save.Visible = true;
            clear.Visible = true;
            button9.Visible = true;
            button12.Visible = true;
            button7.Visible = true;
            button8.Visible = true;

            label1.Visible = true;
            label2.Visible = true;
            label3.Visible = true;
            label4.Visible = true;
            label5.Visible = true;
            label6.Visible = true;

            comboBox1.Visible = true;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            clickedB = true;
            clickedA = false;

            System.Reflection.Assembly myAssembly;
            myAssembly = System.Reflection.Assembly.GetExecutingAssembly();
            System.IO.Stream file;
            Bitmap image;

            file = myAssembly.GetManifestResourceStream("Appsters.background2.png");
            image = new Bitmap(file);
            this.BackgroundImage = image;

            button9.Visible = false;
            button12.Visible = false;
            button7.Visible = false;
            button8.Visible = false;
            button10.Visible = false;
            button13.Visible = false;
            button6.Visible = false;
            button5.Visible = false;
            button11.Visible = false;
            button14.Visible = false;

            label1.Visible = false;
            label2.Visible = false;
            label3.Visible = false;
            label4.Visible = false;
            label5.Visible = false;
            label6.Visible = false;

            comboBox1.Visible = false;
            comboBox2.Visible = false;

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void subj_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (label4.Text == "0")
            {
                label4.Text = "0";
            }
            else
            {
                int n = Int32.Parse(label4.Text);
                n--;
                label4.Text = n.ToString();
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (label4.Text == "9")
            {
                label4.Text = "9";
            }
            else
            {
                int n = Int32.Parse(label4.Text);
                n++;
                label4.Text = n.ToString();
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (button11.Visible && button14.Visible)
            {
                button6.Visible = true;
                button5.Visible = true;
                button11.Visible = false;
                button14.Visible = false;
            }
            else if (button6.Visible && button5.Visible)
            {
                button10.Visible = true;
                button13.Visible = true;
                button5.Visible = false;
                button6.Visible = false;
            }
            else if (button10.Visible && button13.Visible)
            {
                button9.Visible = true;
                button12.Visible = true;
                button10.Visible = false;
                button13.Visible = false;
            }
            else
            {
                button11.Visible = true;
                button14.Visible = true;
                button9.Visible = false;
                button12.Visible = false;
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (label1.Text == "12")
            {
                label1.Text = "12";
            }
            else
            { 
                //convert label to int then increment
                int n = Int32.Parse(label1.Text);
                n++;
                label1.Text = n.ToString();
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            if (label3.Text == "5")
            {
                label3.Text = "5";
            }
            else
            {
                //convert label to int then increment
                int n = Int32.Parse(label3.Text);
                n++;
                label3.Text = n.ToString();
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            if (label5.Text == "PM")
            {
                label5.Text = "AM";
            }
            else
            {
                label5.Text = "PM";
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            if (label1.Text == "1")
            {
                label1.Text = "1";
            }
            else
            {
                //convert label to int then increment
                int n = Int32.Parse(label1.Text);
                n--;
                label1.Text = n.ToString();
            }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            if (label3.Text == "0")
            {
                label3.Text = "0";
            }
            else
            {
                int n = Int32.Parse(label3.Text);
                n--;
                label3.Text = n.ToString();
            }
        }

        private void button14_Click(object sender, EventArgs e)
        {
            if (label5.Text == "PM")
            {
                label5.Text = "AM";
            }
            else
            {
                label5.Text = "PM";
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (button9.Visible && button12.Visible)
            {
                button10.Visible = true;
                button13.Visible = true;
                button9.Visible = false;
                button12.Visible = false;
            }
            else if (button10.Visible && button13.Visible)
            {
                button6.Visible = true;
                button5.Visible = true;
                button10.Visible = false;
                button13.Visible = false;
            }
            else if (button5.Visible && button6.Visible)
            {
                button11.Visible = true;
                button14.Visible = true;
                button5.Visible = false;
                button6.Visible = false;
            }
            else
            {
                button9.Visible = true;
                button12.Visible = true;
                button11.Visible = false;
                button14.Visible = false;

            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.Text == "0")
            {
                comboBox2.Visible = false;
                label6.Visible = false;
            }
            else
            {
                comboBox2.Visible = true;
                label6.Visible = true;
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            save.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255);

            try
            {
                if (title.Text.Length > 0 && subj.Text.Length > 0 && desc.Text.Length > 0)
                {
                    dbConn.Open();

                    string cmdStr = @"INSERT INTO Task VALUES (?, ?, ?, ?, ?, ?, ?)";

                    OleDbCommand cmd = new OleDbCommand(cmdStr, dbConn);

                    cmd.Parameters.AddWithValue("title", title.Text);
                    cmd.Parameters.AddWithValue("subject", subj.Text);
                    cmd.Parameters.AddWithValue("description", desc.Text);

                    if (clickedA == true && clickedB == false && comboBox1.SelectedIndex == 0)
                    {
                        alm = label1.Text + label2.Text + label3.Text + label4.Text + " " + label5.Text;
                        rpt = comboBox1.SelectedItem.ToString();
                        cmd.Parameters.AddWithValue("alarm", alm);
                        cmd.Parameters.AddWithValue("Repeat", rpt);
                        cmd.Parameters.AddWithValue("Interval", "N/A");
                    }
                    else if (clickedA == true && clickedB == false && comboBox1.SelectedIndex != 0)
                    {
                        alm = label1.Text + label2.Text + label3.Text + label4.Text + " " + label5.Text;
                        rpt = comboBox1.SelectedItem.ToString();
                        ivl = comboBox2.SelectedItem.ToString();
                        cmd.Parameters.AddWithValue("alarm", alm);
                        cmd.Parameters.AddWithValue("Repeat", rpt);
                        cmd.Parameters.AddWithValue("Interval", ivl);
                    }
                    else if (clickedB == true && clickedA == false)
                    {
                        cmd.Parameters.AddWithValue("alarm", "No Alarm");
                        cmd.Parameters.AddWithValue("Repeat", "N/A");
                        cmd.Parameters.AddWithValue("Interval", "N/A");
                    }

                    cmd.Parameters.AddWithValue("theDate", date);
                    cmd.ExecuteNonQuery();
                    dbConn.Close();

                    MessageBox.Show("Your task has been saved successfully!");

                    //create alarm instance here
                    drone drn = new drone(lDate, alm, title.Text, subj.Text, desc.Text, rpt, ivl);
                    dv.Show();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Please place a Title, Subject and Description of your task.");
                }    
            } catch (Exception er)
            {

            }
            
        }

        private void backButton_Click(object sender, EventArgs e)
        {
            backButton.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255);
            dv.Show();
            this.Close();
        }

        private void clear_Click(object sender, EventArgs e)
        {
            clear.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255);
            title.Clear();
            subj.Clear();
            desc.Clear();
        }

        private void updateMe()
        {
            save.Hide();
            newSave.Show();
        }

        private void newSave_Click(object sender, EventArgs e)
        {
            newTitle = this.title.Text;
            newSubj = this.subj.Text;
            newDesc = this.desc.Text;

            dbConn.Open();

            string actStr = @"UPDATE Task SET title = ?, subject = ?, description = ?, alarm = ?, repeat = ?, [interval] = ?
                                    WHERE title = '" + oldTitle + "' AND subject = '" + oldSubj + "' AND description = '"
                                                     + oldDesc + "'";
            OleDbCommand c = new OleDbCommand(actStr, dbConn);

            c.Parameters.AddWithValue("title", newTitle);
            c.Parameters.AddWithValue("subject", newSubj);
            c.Parameters.AddWithValue("description", newDesc);
            if (radioButton2.Checked == true)
            {
                c.Parameters.AddWithValue("alarm", "No Alarm");
                c.Parameters.AddWithValue("repeat", "N/A");
                c.Parameters.AddWithValue("[interval]", "N/A");
                c.ExecuteNonQuery();
            }
            else
            {
                c.Parameters.AddWithValue("alarm", label1.Text + label2.Text + label3.Text + label4.Text + " " + label5.Text);
                c.Parameters.AddWithValue("repeat", comboBox1.SelectedItem.ToString());
                c.Parameters.AddWithValue("[interval]", comboBox2.SelectedItem.ToString());
                c.ExecuteNonQuery();
            }
            MessageBox.Show("You have successfuly edited your task!");
            dbConn.Close();
            drone drn = new drone(lDate, alm, title.Text, subj.Text, desc.Text, rpt, ivl);
            dv.Show();
            this.Close();
        }
    }
}
